/*
 * @Date: 2025-01-19 11:51:00
 * @LastEditors: zl 1077167261@qq.com
 * @LastEditTime: 2025-02-10 18:01:38
 * @FilePath: \vue-admin\src\store\modules\user.js
 */
import { loginAPI, getProfileAPI } from "@/api/user";
export default {
  namespaced: true,
  state: () => {
    return {
      // 路由权限管理的数据
      profile: {},
      token: "",
    };
  },
  mutations: {
    setProfile(state, profile) {
      state.profile = profile;
    },
    setToken(state, token) {
      state.token = token;
    },
    clearUserInfo(state) {
      // 清除Token
      state.token = "";
      state.profile = {}
    },
  },
  actions: {
    async getProfile(ctx) {
      const res = await getProfileAPI();
      ctx.commit("setProfile", res.data);
      // return后，可以直接在  const perms = await store.dispatch('user/getUserProfile')
      // 时接收到返回的数据
      return res.data.permissions;
    },
    async doLogin(ctx, { username, password }) {
      // 1. 调用接口
      const res = await loginAPI({ username, password });
      // 2. 提交mutation
      ctx.commit("setToken", res.data.token);
    },
  },
};
